/**
 * Project Name:springmvc
 * File Name:NettyClientHandler.java
 * Package Name:com.netty.client
 * Date:2016年4月30日下午2:01:19
 * Copyright (c) 2016,  All Rights Reserved.
 *
*/

package com.netty.client;

import java.io.UnsupportedEncodingException;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Constant;

/**
 * ClassName:NettyClientHandler <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年4月30日 下午2:01:19 <br/>
 * 
 * @author i1093108915@yeah.net
 * @version
 * @since JDK 1.6
 * @see
 */
public class NettyClientHandler extends ChannelHandlerAdapter {
	private ByteBuf firstMessage;

	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		byte[] data = "服务器，给我一个APPLE".getBytes();
		firstMessage = Unpooled.buffer();
		firstMessage.writeBytes(data);
		ctx.writeAndFlush(firstMessage);
	}

	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf buf = (ByteBuf) msg;
		String rev = getMessage(buf);
		System.out.println("客户端收到服务器数据:" + rev);
	}

	private String getMessage(ByteBuf buf) {
		byte[] con = new byte[buf.readableBytes()];
		buf.readBytes(con);
		try {
			return new String(con, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
}
